package learn.Thread;

//线程交替打印
public class Thread1A2B {
    public static void main(String[] args) throws InterruptedException {
        Object lock = new Object();

        new Thread(new printA(lock)).start();
        Thread.sleep(10);
        new Thread(new print1(lock)).start();



    }

}

class printA extends Thread{
    private Object lock;
    public printA(Object lock){
        this.lock = lock;
    }
    @Override
    public void run(){
        for (int i = 1; i <= 10; i++) {
            synchronized (lock) {
                System.out.println(Thread.currentThread().getName() + ":" + i);
                try {
                    lock.notify();
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }



}
class print1 extends Thread{
    private Object lock;
    public print1(Object lock){
        this.lock = lock;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            synchronized (lock) {
                char ch = (char) (i + 'a');
                System.out.println(Thread.currentThread().getName() + ":" + ch);
                lock.notify();
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
